package boot.exception;

import boot.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: zhiyu
 * @Date:14:14
 * @DESC:
 */
//@ControllerAdvice
@Slf4j
public class MyGlobalExceptionHandler {
    //前后不分离
    @ExceptionHandler(ArithmeticException.class)
    public String error(Exception e){
        log.info("异常信息：{}",e.getMessage());
        return "redirect:/index.html";
    }

    //前后分离@RestControllerAdvice
    @ResponseBody
    @ExceptionHandler(CustomException.class)
    public Result errorByCustom(CustomException cs){
        return new Result(500,cs.getMessage());
    }
    //前后分离 针对非自定义异常  @RestControllerAdvice
    @ExceptionHandler(Exception.class)
    public Result errorByException(Exception ex){
        return new Result(500,"系统崩溃");
    }
}
